const images = [
  'http://localhost:5173/src/assets/1.jpeg',
  'http://localhost:5173/src/assets/2.jpeg',
  'http://localhost:5173/src/assets/3.jpeg',
  'http://localhost:5173/src/assets/4.jpeg',
  'http://localhost:5173/src/assets/5.jpeg',
  'http://localhost:5173/src/assets/6.jpeg',
  'http://localhost:5173/src/assets/7.jpeg',
  'http://localhost:5173/src/assets/8.jpeg',
  'http://localhost:5173/src/assets/9.jpeg',
];

export async function preloadImages() {
  const _images = [...images];

  function loadImage() {
    const src = _images.shift();
    return new Promise<void>((resolve, reject) => {
      const link = document.createElement('link');
      link.rel = 'preload';
      link.as = 'image';
      link.href = src as string;
      document.head.appendChild(link);
      link.onload = resolve as () => void;
      link.onerror = reject;
      setTimeout(resolve, 10000);
    });
  }

  function _loadImage() {
    loadImage().finally(() => {
      if (_images.length) {
        _loadImage();
      }
    });
  }

  for (let i = 0; i < 3; i++) {
    _loadImage();
  }
}
